package org.javaboy.attempts_login_demo.config;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(a -> a.anyRequest().authenticated())
                .formLogin(f -> f
                        .successHandler((req, resp, auth) -> {
                            resp.getWriter().write("success");
                        })
                        .failureHandler((req, resp, e) -> {
                            resp.setContentType("text/html;charset=utf-8");
                            resp.getWriter().write(e.getMessage());
                        })
                        .permitAll())
                .csrf(c -> c.disable());
        return http.build();
    }
}
